package com.xm.servlet.background.home;

import com.xm.entity.AdministratorInformation;

import com.xm.exception.SqlErrorException;
import com.xm.service.background.*;
import lombok.SneakyThrows;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

/**
 * @author 康文程
 * @date 2023-12-14 下午 12:56
 */
@WebServlet("/background/home")
public class HomeServlet extends HttpServlet {
    /**
     * 管理员详细Service
     */
    private final AdministratorInformationService service = new AdministratorInformationService();
    /**
     * 菜单Service
     */
    private final MenuService menuService = new MenuService();
    /**
     * 搜索商品Name
     */
    private String commoditiesName = "";

    /**
     * 主页
     *
     * @param req  an {@link HttpServletRequest} object that
     *             contains the request the client has made
     *             of the servlet
     * @param resp an {@link HttpServletResponse} object that
     *             contains the response the servlet sends
     *             to the client
     */
    @SneakyThrows(Exception.class)
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
        resp.setContentType("text/html;charset=utf-8");
        HttpSession scope = req.getSession();
        //获取账号ID
        String id = (String) scope.getAttribute("id");
        //获取菜单ID
        Integer menuId = Integer.valueOf(req.getParameter("menuId") == null ? "100" : req.getParameter("menuId"));
        //需要跳转的菜单Id
        System.out.println(menuId);

        AdministratorInformation administrator = service.getAdministratorById(id);
        //默认显示主页
        req.setAttribute("menuId", menuId);
        //用户信息ID
        req.setAttribute("administrator", administrator);
        //查询用户拥有权限
        String permissions = administrator.getAdministrator().getPermissions();

        // 如果包含逗号，则认为是一个包含多个权限的字符串，将其拆分成数组
        String[] permissionsArray = permissions.split(",");
        req.setAttribute("permissionsArray", permissionsArray);

        // 如果不包含逗号，则认为是拥有全部权限
        req.setAttribute("permissions", permissions);

        //判断是否跳转到员工管理 阿里巴巴规范手册:规范的写法，使用有意义的常量
        final Integer informationId = 103;
        //判断是否跳转到权限管理
        final Integer permissionsId = 105;
        //判断是否跳转道售后管理
        final Integer afterSalesId = 106;
        //判断是否跳转道仓库管理
        final Integer warehouseId = 101;
        //判断是否跳转道活动管理
        final Integer messageId = 107;
        //判断是否跳转到商品管理
        final Integer commodityId = 102;
        //判断是否跳转到订单管理
        final Integer ordersId = 108;
        //判断是否跳转到物流管理
        final Integer logisticsId = 104;
        //判断是否跳转到设置
        final Integer setId = 110;

        req.setAttribute("menuList", menuService.getAll());
        if (menuId.equals(informationId)) {
            //员工管理处理
            employeeManagement(req);
        } else if (menuId.equals(afterSalesId)) {
            //售后管理处理
            afterSalesManagement(req);
        } else if (menuId.equals(messageId)) {
            //活动管理处理
            eventManagement(req);
        } else if (menuId.equals(commodityId)) {
            //商品管理处理
            commodityManagement(req);
        } else if (menuId.equals(warehouseId)) {
            //仓库管理处理 缺少查询字段
            warehouseManagement(req);
        } else if (menuId.equals(permissionsId)) {
            //权限管理处理
            permissionsManagement(req);
        } else if (menuId.equals(ordersId)) {
            //订单管理处理
            orderManagement(req);
        }else if (menuId.equals(logisticsId)){
            //物流管理处理
            logisticsManagement(req);
        }else if (menuId.equals(setId)) {
            //设置处理
            setManagement(req);
        }else{
            indexManagement(req);
        }
        req.getRequestDispatcher("/WEB-INF/page/background/home.jsp").forward(req, resp);
    }
    /**
     * 如果跳转到首页 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void indexManagement(HttpServletRequest req) throws SqlErrorException {
        final OrdersService service = new OrdersService();
        final AfterSalesService afterSalesService = new AfterSalesService();
        final CommodityWarehouseService commodityWarehouseService = new CommodityWarehouseService();
        final MattersService mattersService = new MattersService();

        commodityWarehouseService.insertMattersFromCommodityWarehouse();
        req.setAttribute("mattersList",mattersService.getAllMatters());
        req.setAttribute("afterSalesNumber",afterSalesService.getAfterSalesCountByStatusNine(4));
        req.setAttribute("numberCompletedOrd",service.getOrdersCountByStatusNine(9));
        req.setAttribute("numberNotShipped",service.getOrdersCountByStatusNine(4));
        req.setAttribute("totalInventoryGoods",commodityWarehouseService.getCommodityWarehouseNine());
    }
    /**
     * 如果跳转到设置 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    private void setManagement(HttpServletRequest req) {
        employeeManagement(req);
    }

    /**
     * 如果跳转到员工管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void employeeManagement(HttpServletRequest req) {
        //员工Service
        final DepartmentsService departmentsService = new DepartmentsService();
        //员工管理Service
        final AdministratorInformationService administratorInformationService =
                new AdministratorInformationService();
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                7 : Integer.parseInt(req.getParameter("pageSize"));
        //查询姓名或Id
        String employeeMc = req.getParameter("employeeMc") == null ?
                null : req.getParameter("employeeMc");
        String departmentId = req.getParameter("departmentId") == null ?
                null : req.getParameter("departmentId");
        String administratorState = req.getParameter("administratorState") == null ?
                null : req.getParameter("administratorState");
        String dateEntry = req.getParameter("dateEntry") == null ?
                "0" : req.getParameter("dateEntry");

        req.setAttribute("dateEntry", dateEntry);
        req.setAttribute("administratorInformationPage",
                administratorInformationService.pagination(pageNumber,
                        pageSize,
                        employeeMc,
                        departmentId,
                        administratorState,
                        dateEntry));
        req.setAttribute("departmentsList", departmentsService.getAll());
    }

    /**
     * 如果跳转到售后管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void afterSalesManagement(HttpServletRequest req) {
        //售后管理
        final AfterSalesService afterSalesService = new AfterSalesService();
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                7 : Integer.parseInt(req.getParameter("pageSize"));
        String afterSalesMc = req.getParameter("afterSalesMc") == null ?
                null : req.getParameter("afterSalesMc");
        String afterSalesType = req.getParameter("afterSalesType") == null ?
                null : req.getParameter("afterSalesType");
        String afterSalesState = req.getParameter("afterSalesState") == null ?
                null : req.getParameter("afterSalesState");
        String afterSalesDate = req.getParameter("afterSalesDate") == null ?
                "0" : req.getParameter("afterSalesDate");
        req.setAttribute("afterSalesDate", afterSalesDate);
        req.setAttribute("afterSalesPage", afterSalesService.pagination(pageNumber,
                pageSize,
                afterSalesMc,
                afterSalesType,
                afterSalesState,
                afterSalesDate
        ));
    }

    /**
     * 如果跳转到活动管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void eventManagement(HttpServletRequest req) {
        //活动Service
        final MessageService messageService = new MessageService();
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                7 : Integer.parseInt(req.getParameter("pageSize"));
        String activityMc = req.getParameter("activityMc") == null ?
                null : req.getParameter("activityMc");
        String activityDate = req.getParameter("activityDate") == null ?
                "0" : req.getParameter("activityDate");
        String dateBegin = req.getParameter("dateBegin") == null ?
                null : req.getParameter("dateBegin");
        String dateEnd = req.getParameter("dateEnd") == null ?
                null : req.getParameter("dateEnd");
        String messageType = req.getParameter("messageType") == null ?
                null : req.getParameter("messageType");
        req.setAttribute("activityDate", activityDate);
        req.setAttribute("messagePage", messageService.pagination(pageNumber, pageSize, activityDate, activityMc, dateBegin, dateEnd, messageType));
    }

    /**
     * 如果跳转到商品管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void commodityManagement(HttpServletRequest req) throws SqlErrorException {
        final ModelService service = new ModelService();
        //商品Service
        final CommodityService commodityService = new CommodityService();
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                4 : Integer.parseInt(req.getParameter("pageSize"));
        //仓库ID/名称
        String name = req.getParameter("name") == null ?
                "" : req.getParameter("name");

        Integer shelves = req.getParameter("selectShelves") == null ? null : Integer.parseInt(req.getParameter("selectShelves"));


        req.setAttribute("modelList",service.findAll());
        req.setAttribute("searchInput", name);

        req.setAttribute("commodityPage", commodityService.pagination(pageNumber, pageSize, name,shelves));
    }

    /**
     * 如果跳转到仓库管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void warehouseManagement(HttpServletRequest req) {
        final WarehouseService warehouseService = new WarehouseService();
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                10 : Integer.parseInt(req.getParameter("pageSize"));
        //仓库ID/名称
        String warehouseMc = req.getParameter("warehouseMc") == null ?
                null : req.getParameter("warehouseMc");
        req.setAttribute("warehousePage", warehouseService.pagination(pageNumber, pageSize, warehouseMc));

        //获取当前页码
        int warehousePageNumber = req.getParameter("warehousePageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("warehousePageNumber"));
        //获取数据展示条数
        int warehousePageSize = req.getParameter("warehousePageSize") == null ?
                7 : Integer.parseInt(req.getParameter("warehousePageSize"));
        //仓库ID
        int warehousesId = req.getParameter("warehouse-id") == null ?
                10 : Integer.parseInt(req.getParameter("warehouse-id"));
        //商品ID/名称
        String commodityMc = req.getParameter("commodityMc") == null ?
                null : req.getParameter("commodityMc");
        if (commodityMc != null) {
            commoditiesName = commodityMc;
        }
        req.setAttribute("productSearch", commoditiesName);
        req.removeAttribute("warehouseCommodityPage");
        req.setAttribute("warehouseCommodityPage", warehouseService.getCondition(warehousePageNumber, warehousePageSize, warehousesId, commoditiesName));
        //状态
        String commodityWindowStatic = req.getParameter("commodityWindowStatic") == null ? "0" : req.getParameter("commodityWindowStatic");
        req.setAttribute("commodityWindowStatic", Integer.parseInt(commodityWindowStatic));
    }

    /**
     * 如果跳转到权限管理 菜单页面
     *
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    public void permissionsManagement(HttpServletRequest req) {
        //获取当前页码
        int pageNumber = req.getParameter("pageNumber") == null ?
                1 : Integer.parseInt(req.getParameter("pageNumber"));
        //获取数据展示条数
        int pageSize = req.getParameter("pageSize") == null ?
                7 : Integer.parseInt(req.getParameter("pageSize"));
        String permissionsMc = req.getParameter("permissionsMc") == null ?
                null : req.getParameter("permissionsMc");
        String menuDefault = req.getParameter("menuDefault") == null ?
                null : req.getParameter("menuDefault");
        req.setAttribute("menuPage", menuService.pagination(pageNumber, pageSize, permissionsMc, menuDefault));
    }

    /**
     * 如果跳转到订单管理 菜单页面
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    @SneakyThrows
    public void orderManagement(HttpServletRequest req){
        //订单Service
        final OrdersService ordersService = new OrdersService();
        //物流公司
        final LogisticsCompaniesService logisticsCompaniesService = new LogisticsCompaniesService();

        int pageNo = req.getParameter("pageNo") == null
                ? 1 : Integer.parseInt(req.getParameter("pageNo"));
        int pageSize = req.getParameter("pageSize") == null
                ? 7 : Integer.parseInt(req.getParameter("pageSize"));
        String sort = req.getParameter("sort") == null ? "ASC" : req.getParameter("sort");
        String state = req.getParameter("state") == null ? "4" : req.getParameter("state");
        String nameOrId = req.getParameter("nameOrId");

        String desc = "DESC";
        if (sort.equals(desc)){
            req.setAttribute("selectSort",0);
        }else{
            req.setAttribute("selectSort",1);
        }
        req.setAttribute("logisticsCompanieList",logisticsCompaniesService.getAllLogisticsCompanies());
        req.setAttribute("orderPage",ordersService.getAllOrdersWithAddressByPage(pageNo,pageSize,sort,state,nameOrId));
    }
    /**
     * 如果跳转到物流管理 菜单页面
     * @param req an {@link HttpServletRequest} object that
     *            contains the request the client has made
     *            of the servlet
     */
    @SneakyThrows
    public void logisticsManagement(HttpServletRequest req){
        //订单Service
        final OrdersService ordersService = new OrdersService();

        int pageNo = req.getParameter("pageNo") == null
                ? 1 : Integer.parseInt(req.getParameter("pageNo"));
        int pageSize = req.getParameter("pageSize") == null
                ? 7 : Integer.parseInt(req.getParameter("pageSize"));
        String sort = req.getParameter("sort") == null ? "ASC" : req.getParameter("sort");
        String state = req.getParameter("state") == null ? "10" : req.getParameter("state");
        String nameOrId = req.getParameter("nameOrId");
        String desc = "DESC";
        if (sort.equals(desc)){
            req.setAttribute("selectSort",0);
        }else{
            req.setAttribute("selectSort",1);
        }
        req.setAttribute("orderPage",ordersService.getAllOrdersByPage(pageNo,pageSize,sort,state,nameOrId));
    }
}
